var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data:{
    orderList: [],
    searchPageNum: 1,
    totalPage:1,
    orderstatus:0,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    wx.setNavigationBarTitle({
      title: "全部订单"
    })
    let status=options.status;
    if (typeof status !="undefined"){
      this.setData({
        orderstatus: options.status
      });
    }
    this.getOrderList();
  },
  onReachBottom: function () {
    let that = this;
    that.setData({
      searchPageNum: that.data.searchPageNum + 1,  //每次触发上拉事件，把searchPageNum+1  
    });
    var searchPageNum = that.data.searchPageNum;
    var totalPage=that.data.totalPage;
    if (searchPageNum<totalPage){
      wx.showLoading({
        title: '加载中...',
      })
      this.getOrderList();
    } else{
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },
  getOrderList(){
    let that = this;
    let user_id = wx.getStorageSync('userId');
    let userdata = { user_id: user_id, page: that.data.searchPageNum,status: that.data.orderstatus};
    util.request(api.OrderList,userdata).then(function (res) {
      if (res.errno == 0) {
        that.setData({
          orderList: that.data.orderList.concat(res.data),
          totalPage: res.totalPage
        })
        wx.hideLoading();
      }
    });
  },
  payOrder(){
    wx.redirectTo({
      url: '/pages/pay/pay',
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})